package com.wlhse.service.impl;

import com.wlhse.dao.CheckItemDao;
import com.wlhse.dao.CheckItemStardardDao;
import com.wlhse.dao.LeaderCheckItemDao;
import com.wlhse.entity.CheckItemPojo;
import com.wlhse.entity.CheckItemStardardPojo;
import com.wlhse.entity.LeaderCheckItemPojo;
import com.wlhse.exception.WLHSException;
import com.wlhse.service.CheckItemService;
import com.wlhse.util.SortCodeUtil;
import com.wlhse.util.TreeUtil;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CheckItemServiceImpl implements CheckItemService {

    @Resource
    private CheckItemDao checkItemDao;

    @Resource
    private CheckItemPojo checkItemPojo;

    @Resource
    private SortCodeUtil sortCodeUtil;

    @Resource
    private CheckItemStardardDao checkItemStardardDao;

    @Resource
    private LeaderCheckItemDao leaderCheckItemDao;

    @Resource
    private TreeUtil treeUtil;

    private static final int checkItemStep = 4;

    @Override
    public String getTreeDto() {
        return NR.r(treeUtil.generalGetChechItemTree(checkItemDao.getAllCheckItemPojo()));
    }

    @Override
    public String getTreeDtoV2() {
        List<CheckItemPojo> list1 = checkItemDao.getAllCheckItemPojo();
        List<LeaderCheckItemPojo> list2 = leaderCheckItemDao.queryLeaderCheckItem();
        return NR.r(treeUtil.getChechItemTreeV2(list1, list2));
    }

    @Override
    @Transactional
    //修改
    public String addCheckItem(String parentCode, String checkItemName) {
        if (StringUtils.isNotBlank(checkItemName))
            throw new WLHSException("输入为空");
        String code = checkItemDao.queryMaxSon(parentCode, checkItemStep);
        if ("".equals(code)) {
            StringBuilder builder = new StringBuilder(parentCode);
            for (int i = 0; i < checkItemStep - 1; i++) {
                builder.append("0");
            }
            builder.append("1");
            code = builder.toString();
        } else {
            code = sortCodeUtil.getMaxCodeString(code);
        }
        checkItemPojo.setStatus("启用");
        checkItemPojo.setCheckItemName(checkItemName);
        checkItemPojo.setCheckItemCode(code);
        int i = checkItemDao.addCheckItemPojo(checkItemPojo);
        StringBuilder other = new StringBuilder(parentCode);
        for (int j = 0; j < checkItemStep; j++) {
            other.append("0");
        }
        //添加其它
        if (null == checkItemDao.queryByCheckItemCode(other.toString())) {
            checkItemPojo.setCheckItemName("其它");
            checkItemPojo.setCheckItemCode(other.toString());
            checkItemDao.addCheckItemPojo(checkItemPojo);
        }
        return NR.r();
    }

    @Override
    @Transactional
    public String updateCheckItem(CheckItemPojo checkItemPojo) {
        String checkItemCode = checkItemPojo.getCheckItemCode();
        String checkItemName = checkItemPojo.getCheckItemName();
        if (!StringUtils.isNotBlank(checkItemCode) && StringUtils.isNotBlank(checkItemName))
            throw new WLHSException("输入为空");
        if (checkItemDao.updateCheckItemPojo(checkItemPojo) <= 0)
            throw new WLHSException("更新失败");
        return NR.r();
    }

    @Override
    @Transactional
    public String deleteCheckItem(CheckItemPojo checkItemPojo) {
        String checkItemCode = checkItemPojo.getCheckItemCode();
        checkItemDao.deleteCheckItemPojo(checkItemCode);
        checkItemStardardDao.deleteCheckItemStardard(checkItemCode);
        return NR.r();
    }

    @Override
    public String getCheckItemContent(String checkItemCode) {
        List<String> content = checkItemStardardDao.getContentByCode(checkItemCode);
        return NR.r(content, 0, 0);
    }

    @Override
    @Transactional
    public String addCheckItemStandard(CheckItemStardardPojo checkItemStardardPojo) {
        Integer serialNO;
        if (checkItemStardardDao.getLastSerialNOByCode(checkItemStardardPojo.getCheckItemCode()) == null) {
            serialNO = 1;
        } else {
            serialNO = checkItemStardardDao.getLastSerialNOByCode(checkItemStardardPojo.getCheckItemCode());
        }
        checkItemStardardPojo.setSerialNo(serialNO);
        if (checkItemStardardDao.addCheckItemStardardPojo(checkItemStardardPojo) <= 0)
            throw new WLHSException("插入失败");
        return NR.r();
    }

    @Override
    @Transactional
    public String deleteCheckItemStandard(String id) {
        if (checkItemStardardDao.deleteCheckItemStardard(id) <= 0)
            throw new WLHSException("删除失败");
        return NR.r();
    }

}
